package com.lixy.demo.spring4_highlight.ch2.el;

import org.apache.commons.io.IOUtils;

import org.springframework.stereotype.Service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;

@Service
@PropertySource("classpath:com/lixy/demo/spring4_highlight/ch2/el/test.properties")
public class ELConfig {

	public void outputResource() {
		try {
			System.out.println( "normal:" + normal );
			System.out.println( "osName:" + osName );
			System.out.println( "randomNumber:" + randomNumber );
			System.out.println( "fromAnother:" + fromAnother );
			
			System.out.println( "testFile:" + IOUtils.toString( testFile.getInputStream() ) );
			System.out.println( "testUrl:" + IOUtils.toString( testUrl.getInputStream() ) );
			System.out.println( "bookName:" + bookName );
			System.out.println( "bookAuthor:" + environment.getProperty( "book.author" ) );
		} catch( Exception e ) {
			e.printStackTrace();
		}
	}
	
	@Bean
	public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	@Value("I Love You!")
	private String normal;
	
	@Value("#{systemProperties['os.name']}")
	private String osName;
	
	@Value("#{ T(java.lang.Math).random() * 100.0 }")
	private double randomNumber;
	
	@Value("#{demoService.another}")
	private String fromAnother;
	
	@Value("classpath:com/lixy/demo/spring4_highlight/ch2/el/test.txt")
	private Resource testFile;
	
	@Value("http://www.baidu.com")
	private Resource testUrl;
	
	@Value("${book.name}")
	private String bookName;
	
	@Autowired
	private Environment environment;
}
